package com.ts.api.http.org.biz;


import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyQunDe;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Date;

/**
 * @ Author 江小游
 * @ Date 2025/6/16 09:51
 */
@ApiPlan(productId = "247", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5QunDe implements ApiOrgPhoneMd5ConvertHandler {


    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.GET);
        apiDataExchangeParameter.setExtendUrl("/" + apiDataExchangeParameter.getPhoneMd5());
        return MapUtils.<String, Object>newHashMap(newMap -> {
            newMap.put("phone", apiDataExchangeParameter.getPhoneMd5());
        });
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        // 实体类
        OrgApplyQunDe applyQunDe = new OrgApplyQunDe();
        // 扩展
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        // 姓名
        applyQunDe.setName(apiApplyParameter.getUserName());
        // 手机号
        applyQunDe.setPhone(apiApplyParameter.getPhone());
        // 地址
        applyQunDe.setDetailAddress(apiApplyParameter.getCity());
        // 需求金额
        applyQunDe.setDemadMoney(apiFieldConverter.getLoanAmountUnit());
        // 全款车
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String car = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", "无车")
                    .when(apiApplyParameter.getCarInfo(), "2", "按揭车")
                    .when(apiApplyParameter.getCarInfo(), "3", "全款车")
                    .when(apiApplyParameter.getCarInfo(), "4", "按揭车")
                    .end();
            applyQunDe.setPattern(car);
        }
        // 品牌
        applyQunDe.setMake("未知");
        // 来源
        applyQunDe.setPage("账户202");
        // 录入时间
        applyQunDe.setCreateTime(new Date());
        // 资产情况
        if (ValidParam.isNotEmpty(apiFieldConverter.getExtendInfo())) {
            StringBuilder stringBuilder = new StringBuilder();
            //  信用记录-逾期记录
            if (ValidParam.isNotEmpty(apiApplyParameter.getOverdue())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getOverdue(), "1", "无逾期")
                        .when(apiApplyParameter.getOverdue(), "2", "有逾期")
                        .when(apiApplyParameter.getOverdue(), "3", "一年以内无逾期")
                        .end();
                stringBuilder.append("信用记录：").append(text).append(";").append("  ");
            }

            //  芝麻分
            if (ValidParam.isNotEmpty(apiApplyParameter.getSesameCredit())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getSesameCredit(), "1", "600分以下")
                        .when(apiApplyParameter.getSesameCredit(), "2", "600~649分")
                        .when(apiApplyParameter.getSesameCredit(), "3", "650~699分")
                        .when(apiApplyParameter.getSesameCredit(), "4", "700分以上")
                        .end();
                stringBuilder.append("芝麻分：").append(text).append(";").append("  ");
            }

            //  职业
            if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getCareer(), "1", "上班族")
                        .when(apiApplyParameter.getCareer(), "2", "自由职业")
                        .when(apiApplyParameter.getCareer(), "3", "企业主（有营业执照）")
                        .when(apiApplyParameter.getCareer(), "4", "公务员或事业单位")
                        .end();
                stringBuilder.append("职业：").append(text).append(";").append("  ");
            }

            //  学历
            if (ValidParam.isNotEmpty(apiApplyParameter.getEducationAttainment())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getEducationAttainment(), "1", "初中及以下")
                        .when(apiApplyParameter.getEducationAttainment(), "2", "中专")
                        .when(apiApplyParameter.getEducationAttainment(), "3", "高中")
                        .when(apiApplyParameter.getEducationAttainment(), "4", "大专")
                        .when(apiApplyParameter.getEducationAttainment(), "5", "本科")
                        .when(apiApplyParameter.getEducationAttainment(), "6", "研究生及以上")
                        .end();
                stringBuilder.append("学历：").append(text).append(";").append("  ");
            }

            //  收入形式
            if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getSalaryPayMethod(), "1", "银行代发打卡")
                        .when(apiApplyParameter.getSalaryPayMethod(), "2", "现金收入")
                        .when(apiApplyParameter.getSalaryPayMethod(), "3", "个人转账")
                        .end();
                stringBuilder.append("收入形式：").append(text).append(";").append("  ");
            }

            //  房产情况
            if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getRealEstateInfo(), "1", "无")
                        .end("有");
                stringBuilder.append("房产情况：").append(text).append(";").append("  ");
            }

            //  车辆情况
            if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getCarInfo(), "1", "无")
                        .end("有");
                stringBuilder.append("车辆情况：").append(text).append(";").append("  ");
            }

            //  保单
            if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getInsurancePolicy(), "1", "无")
                        .end("有");
                stringBuilder.append("保单：").append(text).append(";").append("  ");
            }

            //  社保
            if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getSocialSecurity(), "1", "无")
                        .end("有");
                stringBuilder.append("社保：").append(text).append(";").append("  ");
            }

            //  公积金
            if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getProvidentFund(), "1", "无")
                        .end("有");
                stringBuilder.append("公积金：").append(text).append(";").append("  ");
            }
            applyQunDe.setBundle(stringBuilder.toString());
        }
        // 客户年龄
        applyQunDe.setUserAge(apiApplyParameter.getAge());
        // 车牌
        applyQunDe.setCarNum("未知");
        return JsonUtils.toJson(applyQunDe);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
